package top.zlcode.friend.service.file.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.zlcode.api.file.RemoteFileService;
import top.zlcode.api.file.vo.FileDeleteVO;
import top.zlcode.api.file.vo.FileUploadVO;
import top.zlcode.common.core.domain.R;
import top.zlcode.common.core.enums.ResultCode;
import top.zlcode.friend.domain.file.vo.UploadVO;
import top.zlcode.friend.service.file.IFileService;


@Slf4j
@Service
public class FileServiceImpl implements IFileService {

    @Value("${zlcode.file.bucketName:zlcode-oneoj}")
    private String bucketName;

    private final String USER = "user";

    @Autowired
    private RemoteFileService remoteFileService;

    @Override
    public R<?> upload(MultipartFile file) {
        R<FileUploadVO> res = remoteFileService.upload(file, bucketName, USER);
        if (res.getCode() != ResultCode.SUCCESS.getCode()) {
            return res;
        }
        UploadVO uploadVO = new UploadVO();
        uploadVO.setUrl(res.getData().getRemoteFileUrl());
        uploadVO.setWholeFileName(res.getData().getWholeFileName());
        return R.ok(uploadVO);
    }

    @Override
    public String getRemoteFileUrl(String wholeFileName) {
        R<String> res = remoteFileService.getRemoteUrl(wholeFileName);
        return res.getData();
    }

    @Override
    public boolean delete(String wholeFileName) {
        R<FileDeleteVO> res = remoteFileService.delete(wholeFileName);
        if (res.getCode() != ResultCode.SUCCESS.getCode()) {
            log.error("删除图片异常: {}", res.getData());
            return false;
        }
        return res.getData().getDeleted();
    }
}
